package com.example.myapplication8;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class showxml extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showxml);
        ListView lstv=findViewById(R.id.show1);
        SimpleAdapter adapter=new SimpleAdapter(showxml.this,mylst(),R.layout.lstitem,new String[]{"id","title","A","B","C","D"},new int[]{R.id.num,R.id.contain,R.id.choA,R.id.choB,R.id.choC,R.id.choD});
        lstv.setAdapter(adapter);
        lstv.setEmptyView(null);
        }
    public ArrayList<Map<String,Object>> mylst()
    {
        Resources resources=this.getResources();
        XmlResourceParser xmlResourceParser=resources.getXml(R.xml.test);
        try {
            int j=0;
            ArrayList<Map<String,Object>> lst=new ArrayList<Map<String,Object>>();
            while(xmlResourceParser.next()!=XmlPullParser.END_DOCUMENT)
            {
                String name=xmlResourceParser.getName();
                if(name!=null&&name.equals("choose")) {
                    int count=xmlResourceParser.getAttributeCount();
                    if(count>0)
                    {
                        HashMap<String,Object> item=new HashMap<String,Object>();
                        for(int i=0;i<count;i++) {
                            String attributes = xmlResourceParser.getAttributeName(i);
                            String attriv = xmlResourceParser.getAttributeValue(i);
                            item.put(attributes, attriv);
                        }
                        lst.add(item);
                    }
                }
            }
            xmlResourceParser.close();
            return lst;
        } catch (IOException | XmlPullParserException e) {
            e.printStackTrace();
        }
       return null;
    }
}